********Figure 3 - Indonesia********

*Load data
cd "$reploc/data/created_data"
use indo_full, clear

sort pidlink ifls
drop if ifls==.
keep if panel==1


keep rbuc*  gambleaverse ifls pidlink inflation imp_age panel biryear_impl grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? birprov_impl entrep evermig smoke cashcropyesno 


reshape wide rbuc*  gambleaverse inflation imp_age grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? entrep evermig smoke cashcropyesno, i(pidlink) j(ifls)

local varlist1 rbuc rbucB rbuc_nga rbuc_garefined rbucgaref1a rbucgaref2a rbucgaref3a rbucgaref4a gambleaverse inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 entrep evermig smoke cashcropyesno 


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'5-`var'4
}

*Generate cohort by birthyear variables to cluster on
egen coh_prov_clust=group(biryear_impl birprov_impl)

*Figure
local x rbuc_garefined
local rhsvari oldindoonly
local rhsvaruse _lam0co3dif
local usei grbr

drop if `x'dif==.

local outputvars smokedif evermigdif entrepdif cashcropyesnodif

foreach var in `outputvars' {

local loc=strpos("`var'","dif")
local loc1=`loc'-1

local varib=substr("`var'",1,`loc1')

sum `varib'4, d
local mean`var'indo1=r(mean)


}

local color1rbuc dkgreen
local color2rbuc purple

reg `x'dif `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse' inflationdif, cluster(coh_prov_clust)
predict `x'_pred if `x'dif!=. & `usei'provAvg`rhsvaruse'!=. & `usei'provSD`rhsvaruse'!=. & inflationdif!=. & coh_prov_clust!=.

*Sort predicted rbuc by sign
gen `x'predsignpos=.
replace `x'predsignpos=0 if `x'_pred<0 & `x'_pred!=.
replace `x'predsignpos=1 if `x'_pred>=0 & `x'_pred!=.

*Sort with respect to median within each sign 
gen `x'medsign=.
sum `x'_pred if `x'predsignpos==0, d
local medineg=r(p50)
sum `x'_pred if `x'predsignpos==1, d
local medipos=r(p50)

*Sort by sign and median
replace `x'medsign=1 if `x'predsignpos==0 & `x'_pred<`medineg'
replace `x'medsign=2 if `x'predsignpos==0 & `x'_pred>=`medineg'
replace `x'medsign=3 if `x'predsignpos==1 & `x'_pred<`medipos'
replace `x'medsign=4 if `x'predsignpos==1 & `x'_pred>=`medipos'
sort `x'medsign

local i=0

foreach varib in `outputvars' {
local i=`i'+1

reg `varib' i.`x'predsignpos, vce(bootstrap, reps(100))

matrix h`varib'`pi'dum = r(table)
gen p`i'=h`varib'`pi'dum[4,2]
gen lb`i'=.
gen ub`i'=.
gen mean`i'=.

forval pi=1(1)4 {

reg `varib' if `x'medsign==`pi', vce(bootstrap, reps(100))
replace mean`i'=_b[_cons] if `x'medsign==`pi'

matrix h`varib'`pi' = r(table)

replace lb`i'=h`varib'`pi'[5,1] if `x'medsign==`pi'
replace ub`i'=h`varib'`pi'[6,1] if `x'medsign==`pi'

} 
}

*Now that have mean, lb, ub, can keep one observation
keep `x'medsign lb* ub* p* mean*
duplicates drop `x'medsign, force
reshape long lb ub mean p, i(`x'medsign) j(variable)

gen variablename=""
local i=0
foreach varib in `outputvars' {
local i=`i'+1
replace variablename="`varib'" if variable==`i'
}

gen locat=.

forval pi=1(1)4 {
replace locat=`pi' if `x'medsign==`pi'
}

capture label drop locatlab
label define locatlab 1 "<0 & Below Median" 2 "<0 & Above Median" 3 ">0 & Below Median" 4 ">0 & Above Median"

foreach varib in `outputvars' {

levelsof variablename, local(levs)

foreach l in `levs' {
sum p if variablename=="`l'"
local p`l'indo=round(r(mean),.001)
local mean`varib'indo2=round(`mean`varib'indo1',.001)


if `p`l'indo'==0 {
local p`l'indo=0.0001
}

}


label values locat locatlab

if "`varib'"=="entrepdif" {
local tit "{&Delta} Self-Employment"
local numplpi=0.14
local numplpi2=0.13
local maxy=0.15
}

if "`varib'"=="evermigdif" {
local tit "{&Delta} Migrated"
local numplpi=0.0225
local numplpi2=0.021
local maxy=0.025
}

if "`varib'"=="smokedif" {
local tit "{&Delta} Smoking"
local numplpi=0.075
local numplpi2=0.068
local maxy=0.08
}

if "`varib'"=="cashcropyesnodif" {
local tit "{&Delta} Cash Crops"
local numplpi=0.04
local numplpi2=0.033
local maxy=0.05
}



twoway (bar mean locat if `x'medsign==1 & variablename=="`varib'", color(`color1rbuc'*1)) (bar mean locat if `x'medsign==2 & variablename=="`varib'", color(`color1rbuc'*0.5)) (bar mean locat if `x'medsign==3 & variablename=="`varib'", color(`color2rbuc'*0.5)) (bar mean locat if `x'medsign==4 & variablename=="`varib'", color(`color2rbuc'*1)) (rcap ub lb locat if variablename=="`varib'", lcolor(gs8)) ///
, graphregion(color(white)) xlabel(1 2 3 4, valuelabel labsize(small) noticks) xtitle(" ") ///
legend(off) ///
text(`numplpi' 2.5 "P-value of difference (<0 vs. >0): `p`varib'indo'", place(c)  size(medsmall))  ///
text(`numplpi2' 2.5 "Mean in IFLS4: `mean`varib'indo2'", place(c)  size(medsmall))  ///
subtitle(`tit', position(11) justification(left) size(medsmall)) `addsom' ysc(r(`maxy')) ///

cd "$reploc/results"
graph export Figure3Indonesia`varib'.eps, replace

}



